/**
 * 绘制点实体
 */
export let drawPoint = (feature) => {
  let position = {
    longitude: feature.geometry.coordinates[0],
    latitude: feature.geometry.coordinates[1],
    height: feature.properties.height
  }
  // 经纬度转换为世界坐标
  let cartesian = earth.mouseManager.lonlatToWorld(position);
  if (Cesium.defined(cartesian)) {
    let entity = new Cesium.Entity({
      id: feature.properties.id,
      position: cartesian,
      label: earth.core.entitys.getLabel(feature.properties.name, new Cesium.Cartesian2(0, -15)),
      point: earth.core.entitys.getPoint()
    })
    return entity;
  }
}
/**
 * 绘制圆实体
 */
export let drawCircle = (feature) => {
  let coordinates = feature.geometry.coordinates;
  let radius = feature.properties.radius
  let options = {
    id: feature.properties.id,
    name: '圆形',
    position: Cesium.Cartesian3.fromDegrees(coordinates[0], coordinates[1]),
    point: earth.core.entitys.getPoint(),
    label: earth.core.entitys.getLabel('半径 :' + radius.toFixed(2) + '米', new Cesium.Cartesian2(0, -15)),
    ellipse: {
      semiMinorAxis: radius,
      semiMajorAxis: radius,
      //fill:false,
      outline: false,
      outlineWidth: 10.0,
      outlineColor: Cesium.Color.CHARTREUSE,
      material: Cesium.Color.fromCssColorString('#7FFF00').withAlpha(0.5),
      // height:1
      clampToGround: true
    }
  };
  let entity = new Cesium.Entity(options);
  return entity;
}
/**
 * 绘制矩形
 */
export let drawRact = (feature) => {
  let coordinates = feature.geometry.coordinates;
  let cartographics = coordinates.map(coord => {
    return Cesium.Cartographic.fromDegrees(coord[0], coord[1], 0);
  })
  let rect = Cesium.Rectangle.fromCartographicArray(cartographics);
  rect.east += 0.000001;
  rect.north += 0.000001;

  let options = {
    id: feature.properties.id,
    name: '多边形',
    rectangle: {
      coordinates: rect,
      //fill:false,
      outline: false,
      outlineWidth: 10.0,
      outlineColor: Cesium.Color.CHARTREUSE,
      material: Cesium.Color.fromCssColorString('#7FFF00').withAlpha(0.5),
      clampToGround: true
    }
  };
  let entity = new Cesium.Entity(options);
  return entity;
}

/**
 * 绘制线
 */
export let drawLine = (feature) => {
  let coordinates = feature.geometry.coordinates;
  let positions = coordinates.map(coord => {
    // 经纬度转换为世界坐标
    return earth.mouseManager.lonlatToWorld({ longitude: coord[0], latitude: coord[1] });
  })
  let entity = new Cesium.Entity({
    id: feature.properties.id,
    polyline: earth.core.entitys.getLine(positions, Cesium.Color.CHARTREUSE),
  })
  return entity;
}
/**
 * 绘制多边形
 */
export let drawPolygon = (feature) => {
  let coordinates = feature.geometry.coordinates[0];
  let positions = coordinates.map(coord => {
    // 经纬度转换为世界坐标
    return earth.mouseManager.lonlatToWorld({ longitude: coord[0], latitude: coord[1] });
  })
  let options = {
    id: feature.properties.id,
    name: '多边形',
    polygon: {
      hierarchy: positions,
      // perPositionHeight: true,
      outline: false,
      outlineWidth: 10.0,
      material: Cesium.Color.fromCssColorString('#7FFF00').withAlpha(0.5),
      outlineColor: Cesium.Color.CHARTREUSE,
      clampToGround: true
    }
  };
  let entity = new Cesium.Entity(options);
  return entity;
}

/**
 * 绘制通用模型
 */
export function drawModels(featureCollection) {
  return featureCollection.features.map(feature => {
    let position = {
      longitude: feature.geometry.coordinates[0],
      latitude: feature.geometry.coordinates[1],
      alt: feature.geometry.coordinates[2]
    }

    let orientation = feature.geometry.orientation
    let cartesian = Cesium.Cartesian3.fromDegrees(position.longitude, position.latitude, position.alt);
    let entity = new Cesium.Entity({
      id: feature.properties.id,
      type: earth.objectsManager.modelTypes.model,
      position: cartesian,
      orientation: orientation,
      model: {
        // color: Cesium.Color.CHARTREUSE,
        uri: feature.properties.modelUrl || me.modelUri
      }
    });
    return entity;
  })
}